package com.cuitrip.mode;

import com.cuitrip.util.TextUtil;
import org.dom4j.Element;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by MiJack on 2015/11/21.
 */
public class Rule {
    String name;
    String dir;
    String fileSample;
    String commitSample;
    Map<String, String> valueSamples;
    String fileHead;
    String fileEnd;
    KeyTransform transform;
    Map<String, String> replaceMap;

    private Rule() {
        replaceMap = new HashMap<String, String>();
        valueSamples = new HashMap<String, String>();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDir() {
        return dir;
    }

    public void setDir(String dir) {
        this.dir = dir;
    }

    public String getFileSample() {
        return fileSample;
    }

    public void setFileSample(String fileSample) {
        this.fileSample = fileSample;
    }

    public String getCommitSample() {
        return commitSample;
    }

    public void setCommitSample(String commitSample) {
        this.commitSample = commitSample;
    }

    public Map<String, String> getValueSamples() {
        return valueSamples;
    }

    public void setValueSamples(Map<String, String> valueSamples) {
        this.valueSamples = valueSamples;
    }

    public KeyTransform getTransform() {
        return transform;
    }

    public void setTransform(KeyTransform transform) {
        this.transform = transform;
    }

    public String getFileHead() {
        return fileHead;
    }

    public void setFileHead(String fileHead) {
        this.fileHead = fileHead;
    }

    public String getFileEnd() {
        return fileEnd;
    }

    public void setFileEnd(String fileEnd) {
        this.fileEnd = fileEnd;
    }

    public Map<String, String> getReplaceMap() {
        return replaceMap;
    }

    public void setReplaceMap(Map<String, String> replaceMap) {
        this.replaceMap = replaceMap;
    }

    @Override
    public String toString() {
        return "Rule{" +
                "name='" + name + '\'' +
                ", dir='" + dir + '\'' +
                ", \n\t\tfileSample='" + fileSample + '\'' +
                ", commitSample='" + commitSample + '\'' +
                ", valueSamples='" + valueSamples + '\'' +
                ", \n\t\tfileHead='" + fileHead + '\'' +
                ", \n\t\tfileEnd='" + fileEnd + '\'' +
                ",\nreplaceMap=" + TextUtil.mapToString(replaceMap) +
                "\n}";
    }

    public static Rule build(Element e) {
        Rule rule = new Rule();
        rule.name = e.attributeValue("name");
        rule.dir = e.attributeValue("dir");
        rule.fileSample = e.attributeValue("filesample");
        rule.fileHead = e.elementText("file-head");
        rule.fileEnd = e.elementText("file-end");
        rule.commitSample = e.elementText("commit-sample");
        Element valueSamples = e.element("values-samples");
        for (Element element : (List<Element>) valueSamples.elements()) {
            rule.valueSamples.put(element.getName(), element.getTextTrim());
        }
        String text = e.elementText("key-transform");
        if (TextUtil.isEmpty(text)) {
            rule.transform = KeyTransform.NONE;
        } else {
            rule.transform = KeyTransform.valueOf(text.toUpperCase());
        }
        Element replaces = e.element("replaces");
        if (replaces != null) {
            for (Element element : (List<Element>) replaces.elements()) {
                rule.replaceMap.put(element.attributeValue("src"), element.attributeValue("dis"));
            }
        }
        return rule;
    }

    public String getOutputSample(String key) {
        String result = valueSamples.get(key);
        if (TextUtil.isEmpty(result)) {
            result = valueSamples.get("default");
        }
        return result;
    }
}
